package com.xg.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

    @RequestMapping(value = "/subLogin",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String subLogin(HttpServletRequest httpServletRequest){

        String username=httpServletRequest.getParameter("username");
        String password=httpServletRequest.getParameter("password");
        String rememberMe=httpServletRequest.getParameter("rememberMe");

        System.out.println(username+"**********"+password);

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        try {
            if(rememberMe!=null){

                token.setRememberMe(true);

            }
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return  e.getMessage();
        }

        if(subject.hasRole("admin")){
            System.out.println("***********有admin权限*************");
        }

        return "登录成功";
    }

    //@RequiresRoles("admin")
    @RequestMapping(value = "/testRole", method = RequestMethod.GET)
    @ResponseBody
    public String testRole() {
        return "testRole success";
    }

    //    @RequiresRoles("admin1")
    //@RequiresPermissions("admin1")
    @RequestMapping(value = "/testRole1", method = RequestMethod.GET)
    @ResponseBody
    public String testRole1() {
        return "testRole1 success 1";
    }
    @RequestMapping(value = "/testPerms", method = RequestMethod.GET)
    @ResponseBody
    public String testPerms() {
        return "testPerms success";
    }

    @RequestMapping(value = "/testPerms1", method = RequestMethod.GET)
    @ResponseBody
    public String testPerms1() {
        return "testPerms1 success";
    }


}
